package com.mzy.linear_struct;

import com.mzy.linear_struct.utils.Utils;
import sun.reflect.generics.tree.Tree;

import java.util.*;

/**
 * @Author: codermzy
 * @Date: 2024/03/31/11:34
 * @Description:
 */
public class Test {

    public static final Object lock = new Object();

    public static boolean printNum = false;


    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (lock){
                for (int i = 1; i <= 3; i++) {
                    while (printNum){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println(i);
                    printNum = true;
                    lock.notifyAll();
                }
            }
        }).start();


        new Thread(() -> {
            synchronized (lock){
                for (char c = 'A'; c <= 'C'; c++){
                    while (!printNum){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    System.out.println(c);
                    printNum = false;
                    lock.notifyAll();
                }
            }
        }).start();


        Utils.testSort(Test::quickSort);

        System.out.println(testKH("]})"));
    }

    public static boolean testKH(String s) {
        Stack<Character> stack = new Stack<>();
        // )))
        for (char c : s.toCharArray()) {
            if('(' == c){
                stack.push(')');
            }else if('{' == c){
                stack.push('}');
            }else if('[' == c){
                stack.push(']');
            }else {
                if(stack.isEmpty() || stack.pop() != c){
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }

    private static void quickSort(Integer[] arr) {
        partition(arr, 0, arr.length - 1);
    }

    private static void partition(Integer[] arr, int left, int right) {
        if(left >= right){
            return;
        }

        int pivot = arr[right];
        int i = left;
        int j = right - 1;

        while (i <= j){
            while (arr[i] < pivot){
                i++;
            }

            while (j >= 0 && arr[j] > pivot){
                j--;
            }

            if(i <= j){
                Utils.swap(arr, i, j);
                i++;
                j--;
            }
        }

        Utils.swap(arr, i, right);
        partition(arr, left, j);
        partition(arr, i + 1, right);
    }
}
